package com.atguigu.web;

import com.atguigu.pojo.*;
import com.atguigu.service.Impl.OrderServiceImpl;
import com.atguigu.service.OrderService;
import com.atguigu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName OrderServlet
 * @Description 订单的相关操作
 * @Author 周轩霆
 * @Date 2022/4/29 13:39
 **/

public class OrderServlet extends BaseServlet {
    OrderService orderService = new OrderServiceImpl();

    /**
     * @return
     * @Author 周轩霆
     * @Description 用于生成订单
     * @Date 13:40 2022/4/29
     * @Param
     */
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //调用
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        User loginUser = (User) req.getSession().getAttribute("user");
        if (loginUser == null) {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
            //登录完之后结束，不再往下执行
            return;
        }
        //调用方法，返回订单号
        Integer userId = loginUser.getId();
        String orderId = orderService.createOrder(cart, userId);
        // req.setAttribute("orderId",orderId);
        req.getSession().setAttribute("orderId", orderId);
        //请求重定向防止重复提交
        //req.getRequestDispatcher("/pages/cart/checkout.jsp").forward(req,resp);
        resp.sendRedirect(req.getContextPath() + "/pages/cart/checkout.jsp");
    }

    /**
     * @return void
     * @Author 周轩霆
     * @Description 查询全部订单
     * @Date 14:49 2022/4/29
     * @Param [req, resp]
     */
    protected void showAllOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Order> orders = orderService.showAllOrders();
        if (orders != null) {
            req.setAttribute("orders", orders);
            req.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(req, resp);
        } else {
            //没有订单时转发到制定页面
            req.getRequestDispatcher("/pages/manager/order_null.jsp").forward(req, resp);
        }
    }

    /**
     * @return void
     * @Author 周轩霆
     * @Description 关于发货的Servlet
     * @Date 16:46 2022/4/30
     * @Param [req, resp]
     */
    protected void sendOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //当前台点击 一键发货按钮时，需要修改订单的状态，并显示
        //获取请求参数
        String orderId = req.getParameter("orderId");
        //更改对应订单号订单的状态
        orderService.sendOrder(orderId);
        //修改完之后调用查询所有订单的方法，因为数据库中状态已经修改，所以可以直接查询
        showAllOrders(req,resp);
    }

 /*   protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求的参数 pageNo,pageSize
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //2.调用bookService.page(pageNo,pageSize)方法:page对象
        Page<Book> page = orderService.page(pageNo, pageSize);
        page.setUrl("manager/bookServlet?action=page");
        //3.保存page对象到Request域中
        req.setAttribute("page", page);
        //4.请求转发到pages/manager/book_manager.jsp
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);

    }*/

}
